<?php

class SavingsAccounts extends HTech_Db_Table
{
    protected $_name    = 'view_savings_accounts';
    protected $_primary = 'SavingsAccountId';  
    
    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Savings accounts");
        $this->_gridMetadata['onClick'] = 'admin.client.view';
        $this->_gridMetadata['headers'] = array
            (
                array('SavingsAccountId',     50, $this->translate("Id"),                      'Number'),
                array('SavingsProductName',  100, $this->translate("Type"),                 'String'),
                array('ClientFullName',      200, $this->translate("Client"),                  'String'),
                array('ClientCode',          200, $this->translate("Code"),            'String'),
                array('BankName',            250, $this->translate("Bank"),            'String'),
                array('Balance',             150, $this->translate("Balance"),   'Number',   'Standard'),
                array('OfficerUsername',     150, $this->translate("Officer"),            'String'),
                array('LocationShortName',   150, $this->translate("Branch"),            'String'),
                array('CommunityType',       150, $this->translate("Community"),            'String')
            );
        return $this;
    }
    
    public static function calcAccruedInterest($account)
    {
        if($account->d->Balance >= $account->d->MaxBalance) {
            $tasa = $account->d->MaxRate;
        } elseif($account->d->Balance < $account->d->MinBalance) {
            $tasa = 0;
        } else {
            $m = Log($account->d->Balance - $account->d->MinBalance + 1)/Log($account->d->MaxBalance-$account->d->MinBalance + 1);
            $tasa = $m*($account->d->MaxRate - $account->d->MinRate) + $account->d->MinRate;
        }
        $d1 = new DateTime($account->d->LastTransactionDate);
        $d2 = new DateTime(date('Y-m-d'));
        return $account->d->AccruedInterest + $account->d->Balance*($tasa/(100*365))*$d1->diff($d2)->days;
    }
}